package com.atguigu.springcloud.controller;

import com.atguigu.springcloud.entities.CommonResult;
import com.atguigu.springcloud.entities.Payment;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

@RestController
@Slf4j
public class OrderController {
    @Autowired
    private RestTemplate restTemplate;

    //public final static String PAYMENT_URL = "http://localhost:8001";
    public final static String PAYMENT_SERVICE = "http://CLOUD-PAYMENT-SERVICE";

    @RequestMapping(value = "/consumer/payment/creat")
    public CommonResult<Payment> create(Payment payment) {
        return restTemplate.postForObject(PAYMENT_SERVICE + "/payment/creat", payment, CommonResult.class);
    }

    @GetMapping(value = "/consumer/getpayment/{id}")
    public CommonResult<Payment> getPayment(@PathVariable("id") int id) {
        return restTemplate.getForObject(PAYMENT_SERVICE + "/payment/get/" + id, CommonResult.class);
    }

    @GetMapping(value = "/consummer/payment/getforentity/{id}")
    public CommonResult<Object> getPayment(@PathVariable("id") Long id) {
        ResponseEntity<CommonResult> entity = restTemplate.getForEntity(PAYMENT_SERVICE + "/payment/get/" + id, CommonResult.class);
         if(entity.getStatusCode().is2xxSuccessful()){
             log.info("状态码:"+entity.getHeaders());
             return entity.getBody();
         }
        return new CommonResult<>(444,"操作失败",null);
    }

    @GetMapping("/consumer/payment/zipkin")
    public String paymentZipkin()
    {
        String result = restTemplate.getForObject("http://localhost:8001"+"/payment/payment/zipkin/", String.class);
        return result;
    }
}
